package eclipse.util.maptools;
import javax.swing.*;
import eclipse.util.io.*;
/** Sets of events that the MapToolkit class might fire*/
public class MapEvents 
{
	/** Loads a set of maps that are all related in a way */
	public static void loadWorld()
	{
		int maps;
		if(!IOTools.fileExists(".world"))
			IOTools.makeFile(".world");
		PropertyFile pFile = new PropertyFile(".world");
		String in = pFile.readProperty("WORLD", "MAPCOUNT");
		System.out.println("Loading maps: (0 to " + in + ")");
		if(in != null)
			maps = Integer.parseInt(in);
		else
			maps = 0;
		MapEditor.setMapCount(maps);
		for(int i = 0; i < maps; i++)
			loadMap(i);
	}
	/** Builds a world pack which contains maps that are closely related. */
	public static void saveWorld()
	{
		if(IOTools.fileExists(".world"))
			IOTools.killFile(".world");
		IOTools.makeFile(".world");
		PropertyFile pFile = new PropertyFile(".world");
		pFile.writeProperty(String.valueOf(MapEditor.map.length), "WORLD", "MAPCOUNT");
		for(int i = 0; i < MapEditor.map.length; i++)
			saveMap(i);
	}	
	/** Loads a single map */
	public static void loadMap(int mapNum)
	{
		if(!IOTools.fileExists("map" + mapNum))
			return;
		byte[] byteArray = IOTools.readAsBytes("map" + mapNum);
		MapEditor.map[mapNum].getData().setArray(byteArray);
		MapEditor.map[mapNum].decode(byteArray);
	}
	/** Saves a single map */
	public static void saveMap(int mapNum)
	{
		if(IOTools.fileExists("map" + mapNum))
			IOTools.killFile("map" + mapNum);
		IOTools.makeFile("map" + mapNum);
		byte[] byteArray = MapEditor.map[mapNum].getData().toArray();
		IOTools.writeBytesToFile("map" + mapNum, byteArray);
	}
	/** Sets the maximum layers for a map*/
	public static void setLayers()
	{
		int  layerCount;
		String opt = "" + JOptionPane.showInputDialog(null, "Max layers");
		if(opt.length() != 0)
			layerCount = Integer.parseInt(opt);
		else
		{
			JOptionPane.showMessageDialog(null, "You must input a number!");
			return;
		}
		layerCount --;
		MapEditor.map[MapEditor.currentMap].writeLayerCount(layerCount);
		MapEditor.map[MapEditor.currentMap].decode(MapEditor.map[MapEditor.currentMap].getData());
		updateCaption();
	}
	/** Shifts the current layer value to the next layer */
	public static void shiftLayerNext()
	{
		if(MapEditor.map[MapEditor.currentMap].getLayerCount() > MapEditor.currentLayer)
			MapEditor.currentLayer ++;
		else
			JOptionPane.showMessageDialog(null, "There are no more layers!");
		updateCaption();
	}
	/** Shifts the current layer to the previous layer */
	public static void shiftLayerPrevious()
	{
		if(MapEditor.currentLayer > 0)
			MapEditor.currentLayer --;
		else
			JOptionPane.showMessageDialog(null, "There are no more layers!");
		updateCaption();
	}
	/** Updates the caption displaying the current map and current layer */
	public static void updateCaption()
	{
		String title;
		title = "Map: " + MapEditor.currentMap + " Layer: " + MapEditor.currentLayer; 
		MapEditor.window.setTitle(title);
	}
	/** Deletes all data about a single layer */
	public static void deleteLayer()
	{
		MapEditor.toolkit.input[10].setText("0");
		MapEditor.toolkit.input[11].setText("0");
		reformatLayer();
	}
	/** Readjusts a layer's dimensions and location */
	public static void reformatLayer()
	{		
		@SuppressWarnings("unused")
		int map, layer, xVal, yVal, xPos, yPos, zPos;
		String opt;
		
		map = MapEditor.currentMap;
		layer = MapEditor.currentLayer;
		
		opt = MapEditor.toolkit.input[10].getText();
		if(opt.length() != 0)
			xVal = Integer.parseInt(opt);
		else
		{
			JOptionPane.showMessageDialog(null, "You must input a number!");
			return;
		}
		opt = MapEditor.toolkit.input[11].getText();
		if(opt.length() != 0)
			yVal = Integer.parseInt(opt);
		else
		{
			JOptionPane.showMessageDialog(null, "You must input a number!");
			return;
		}		
		opt = MapEditor.toolkit.input[7].getText();
		if(opt.length() != 0)
			xPos = Integer.parseInt(opt);
		else
		{
			JOptionPane.showMessageDialog(null, "You must input a number!");
			return;
		}
		opt = MapEditor.toolkit.input[8].getText();
		if(opt.length() != 0)
			yPos = Integer.parseInt(opt);
		else
		{
			JOptionPane.showMessageDialog(null, " You must input a number!");
			return;
		}
		opt = MapEditor.toolkit.input[8].getText();
		if(opt.length() != 0)
				zPos = Integer.parseInt(opt);
		else
		{
			JOptionPane.showMessageDialog(null, "You must input a number!");
			return;
		}
		MapEditor.map[map].writeLayerDim(layer, xVal, yVal);
		
		MapEditor.map[map].decode(MapEditor.map[map].getData());
	}
}

